package zeebe.export;

import io.vertx.core.json.JsonObject;
import io.zeebe.client.ZeebeClient;
import io.zeebe.client.api.response.ActivatedJob;
import io.zeebe.client.api.worker.JobClient;
import io.zeebe.client.api.worker.JobHandler;

public class CalculateJobWorkerHandler implements JobHandler {
    ZeebeClient zeebeClient;

    public CalculateJobWorkerHandler(ZeebeClient client) {
        this.zeebeClient = client;
    }

    @Override
    public void handle(JobClient client, ActivatedJob job) throws Exception {
        System.out.println("开始执行CalculateJobWorker");

        JsonObject data = new JsonObject(job.getVariables());
        int numberOne = data.getInteger("NumberOne");
        int numberTwo = data.getInteger("NumberTwo");

        int offset = Integer.parseInt(job.getCustomHeaders().get("offset"));
        int numberThree = sayHello(numberOne, numberTwo, offset);
        System.out.println("流程执行的运算结果是：" + numberThree);

        JsonObject output = new JsonObject()
                .put("NumberThree", numberThree);

        client.newCompleteCommand(job.getKey())
                .variables(output.encode())
                .send()
                .join();
    }

    private int sayHello(int numberOne, int numberTwo, int offset) {
        return numberOne + numberTwo + offset;
    }
}
